package com.cxj.link;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cxj.link.util.ToastUtil;
import com.cxj.link.view.GameLogic;
import com.cxj.link.view.GameLogicImpl;
import com.cxj.link.view.GameView;
import com.cxj.link.view.Cut;
import com.cxj.link.vo.GameConfig;
import com.cxj.link.vo.LinkVo;

public class Linker extends Activity {
    private GameConfig config;
    private GameConfig config1;
    private GameConfig config2;
    private GameConfig config3;
    private GameLogic gameService;
    private GameView gameView;
    private Button levelBtn;
    private Button promptBtn;
    private Button refreshBtn;
    private Cut selected = null;
    private SharedPreferences sp;
    private AlertDialog.Builder successDialog;
    private TextView timeTextView;

    private void changeLevel() {
        String[] arrayOfString = new String[3];
        arrayOfString[0] = "简单";
        arrayOfString[1] = "中等";
        arrayOfString[2] = "困难";
        new AlertDialog.Builder(this).setTitle("选择难度").setItems(arrayOfString, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                if (paramInt == 0) {
                    Linker.this.config = Linker.this.config1;
                    Setting.level1 = 1;
                }
                if (paramInt == 1) {
                    Linker.this.config = Linker.this.config2;
                    Setting.level1 = 2;
                }
                if (paramInt == 2) {
                    Linker.this.config = Linker.this.config3;
                    Setting.level1 = 3;
                }
                SharedPreferences.Editor localEditor = Linker.this.sp.edit();
                localEditor.putInt("level1", Setting.level1);
                localEditor.commit();
                Linker.this.gameService.setConfig(Linker.this.config);
                Linker.this.startGame();
                paramDialogInterface.dismiss();
            }
        }).create().show();
    }

    private AlertDialog.Builder createDialog(String paramString1, String paramString2) {
        return new AlertDialog.Builder(this).setTitle(paramString1).setMessage(paramString2);
    }

    private void gameViewTouchDown(MotionEvent paramMotionEvent) {
        Cut[][] arrayOfPiece = this.gameService.getPieces();
        float f1 = paramMotionEvent.getX();
        float f2 = paramMotionEvent.getY();
        Cut localPiece = this.gameService.findPiece(f1, f2);
        if (localPiece != null) {
            SoundsManager.playSound(this, 1);
            this.gameView.setSelectedPiece(localPiece);
            if (this.selected != null) {
                if (this.selected != null) {
                    LinkVo localLinkInfo = this.gameService.link(this.selected, localPiece);
                    if (localLinkInfo != null) {
                        SoundsManager.playSound(this, 2);
                        handleSuccessLink(localLinkInfo, this.selected, localPiece, arrayOfPiece);
                    } else {
                        this.selected = localPiece;
                        this.gameView.setPromptPiece(null);
                        this.gameView.postInvalidate();
                    }
                }
            } else {
                this.selected = localPiece;
                this.gameView.postInvalidate();
            }
        }
    }

    private void gameViewTouchUp(MotionEvent paramMotionEvent) {
        this.gameView.postInvalidate();
    }

    private void handleSuccessLink(LinkVo paramLinkInfo, Cut paramPiece1, Cut paramPiece2,
            Cut[][] paramArrayOfPiece) {
        this.gameView.setLinkInfo(paramLinkInfo);
        this.gameView.setSelectedPiece(null);
        this.gameView.postInvalidate();
        paramArrayOfPiece[paramPiece1.getIndexX()][paramPiece1.getIndexY()] = null;
        paramArrayOfPiece[paramPiece2.getIndexX()][paramPiece2.getIndexY()] = null;
        this.selected = null;
        if (!this.gameService.hasPieces())
            this.successDialog.show();
    }

    private void init() {
        if (Setting.level1 != 1) {
            if (Setting.level1 != 2) {
                if (Setting.level1 == 3)
                    this.config = this.config3;
            } else
                this.config = this.config2;
        } else
            this.config = this.config1;
        this.gameView = ((GameView) findViewById(R.id.gameView));
        this.timeTextView = ((TextView) findViewById(R.id.timeText));
        this.timeTextView.setText("无限模式");
        this.refreshBtn = ((Button) findViewById(R.id.refresh_btn));
        this.promptBtn = ((Button) findViewById(R.id.prompt_btn));
        this.levelBtn = ((Button) findViewById(R.id.level_btn));
        this.refreshBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View paramView) {
                SoundsManager.playSound(Linker.this, 5);
                Linker.this.refresh();
            }
        });
        this.promptBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View paramView) {
                Linker.this.prompt();
            }
        });
        this.levelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View paramView) {
                Linker.this.changeLevel();
            }
        });
        this.gameService = new GameLogicImpl(this.config);
        this.gameView.setGameService(this.gameService);
        this.gameView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                if (paramMotionEvent.getAction() == 0)
                    Linker.this.gameViewTouchDown(paramMotionEvent);
                if (paramMotionEvent.getAction() == 1)
                    Linker.this.gameViewTouchUp(paramMotionEvent);
                return true;
            }
        });
        this.successDialog = createDialog("提示", "游戏胜利！ 重新开始").setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        Linker.this.startGame();
                    }
                });
    }

    private void prompt() {
        Cut[] arrayOfPiece = this.gameService.getTwoPieces();
        if (arrayOfPiece != null)
            this.gameView.setPromptPiece(arrayOfPiece);
        else
            ToastUtil.makeText(this, "没有找到可消除的,请刷新");
        this.gameView.postInvalidate();
    }

    private void refresh() {
        this.gameService.refreshPieces();
        this.gameView.postInvalidate();
    }

    private void startGame() {
        this.gameView.startGame();
        this.selected = null;
    }

    @Override
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        requestWindowFeature(1);
        setContentView(2130903040);
        this.sp = getSharedPreferences("setting", 0);
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        this.config1 = new GameConfig(6, 6, localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels, 100, this);
        this.config2 = new GameConfig(8, 8, localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels, 100, this);
        this.config3 = new GameConfig(10, 10, localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels, 100,
                this);
        init();
        startGame();
    }
}
